<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>收羊皮</title>
    <script type="text/javascript" src="layuiadmin/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="js/common.js"></script>
    <link rel="stylesheet" href="layuiadmin/layui/css/layui.css">
    <link rel="stylesheet" href="layuiadmin/style/admin.css">
</head>
<body>
<div style="text-align: center">
    <h1>欢迎【<span id="myName"></span>】一起来游戏，请【庄家】选择游戏模式</h1>
    <div id="isChecked" style="display:none;">
        <label style="font-size: 30px;">游戏模式</label>
        <div>
            <input type="radio" style="zoom:2;" name="model" value="1" title="收羊皮" checked="true">收羊皮
            <input type="radio" style="zoom:2;" name="model" value="2" title="梦幻拖拉机">梦幻拖拉机
        </div>
    </div>
    <!--<button id="aaa">.</button>-->
    <div style="margin-top: 50px;">
        <button class="layui-btn layui-btn-normal" id="qiangzhuangjia">抢庄家</button>
        <button style="display: none" class="layui-btn layui-btn-normal" id="startGaming">开始游戏</button>
    </div>
    <div id="myWindowShow" style="margin-top: 50px;display: none;">
        <a id="zhuang" style="font-size: 20px;">进入庄家页面</a>
        <a id="min" style="font-size: 20px;">进入平民页面</a>
    </div>
</div>
</body>
<script type="text/javascript">
    var username = '';
    var zhuangUrl = '';
    var minUrl = '';
    var role = '';
    var registered = [];
    layui.use('layer', function(){ //独立版的layer无需执行这一句
        var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
        username = getUrlParameter('name');
        if(!username || username == '' || username == 'undefined'){
            layer.msg("用户信息为空！等管理员为您分配账号！！");
            return;
        }
        $('#myName').text(username);

        // 抢庄家操作
        $('#qiangzhuangjia').on('click',function (){
            var curTotal = getTotal();
            if(parseInt(curTotal) > 0 && parseInt(curTotal) < 55){
                layer.alert("游戏已经开始，只有庄家解散了游戏才能重新开始！！！");
                return;
            }

            $.ajax({
                url:'isAdmin?name=' + username,
                type:'get',
                //dataType:'json',
                async:false,
                success:function(res){

                    var flag = userRegister(layer);
                    if(!flag){
                        layer.msg("该用户重复登录！！");
                        return;
                    }
                    if(res.code == 200){
                        layer.msg('恭喜你，你是庄家！！！');
                        role = '1';
                        //var zhuangUrl = 'bookmaker.html?role=1&name=' + username + '&selfCard=' + selfCard;
                        zhuangUrl = 'bookmaker.html?role='+ role +'&name=' + username;
                        $('#qiangzhuangjia').hide();
                        $('#isChecked').show();
                        $('#startGaming').show();
                    }else if(res.code == 500){

                        layer.msg('庄家被人抢走了！！！');
                        role = '2';
                        //var minUrl = 'civilian.html?role=2&name=' + username + '&selfCard=' + selfCard;
                        minUrl = 'civilian.html?role=' + role + '&name=' + username;
                        $('#qiangzhuangjia').hide();
                        $('#isChecked').remove();
                        $('#startGaming').show();
                    }
                },
                error: function(msg){}
            });
        });

        $('#startGaming').on('click',function (){
            var curTotal = getTotal();
            if(parseInt(curTotal) > 0 && parseInt(curTotal) < 55){
                layer.alert("游戏已经开始，只能等庄家解散了游戏才能重新开始！！！");
                return;
            }
            if(role == '1') {
                var checkedModel = $('input[name="model"]:checked').val();
                var checked = isChecked(checkedModel);
                zhuangUrl = 'bookmaker.html?role='+ role +'&name=' + username;
                zhuangUrl = zhuangUrl + '&model=' + checked;
                $('#zhuang').attr('href',zhuangUrl);
                $('#min').hide();
                $('#myWindowShow').show();
            } else if(role == '2'){
                var s = isChecked("ss");
                if(!s || s == 'undefined'){
                    layer.msg("请稍等让庄家选择一种游戏模式！");
                    return;
                }
                minUrl = 'civilian.html?role=' + role + '&name=' + username;
                minUrl = minUrl + '&model=' + s;
                $('#min').attr('href',minUrl);
                $('#zhuang').hide();
                $('#myWindowShow').show();
            }
            /*$.ajax({
                url:'isAdmin?name=' + username,
                type:'get',
                //dataType:'json',
                async:false,
                success:function(res){
                    var curTotal = getTotal();
                    userRegister();
                    if(res.code == 200){
                        layer.msg('恭喜你，你是庄家！！！');
                        //var zhuangUrl = 'bookmaker.html?role=1&name=' + username + '&selfCard=' + selfCard;
                        var zhuangUrl = 'bookmaker.html?role=1&name=' + username + "&model=" + checked;
                        $('#zhuang').attr('href',zhuangUrl);
                        $('#min').hide();
                        $('#myWindowShow').show();
                    }else if(res.code == 500){
                        if(parseInt(curTotal) > 0 && parseInt(curTotal) < 54){
                            layer.alert("妈的，游戏已经开始，不让进！！！");
                            return;
                        }
                        layer.msg('庄家被人抢走了！！！');
                        //var minUrl = 'civilian.html?role=2&name=' + username + '&selfCard=' + selfCard;
                        var minUrl = 'civilian.html?role=2&name=' + username + "&model=" + checked;
                        $('#min').attr('href',minUrl);
                        $('#zhuang').hide();
                        $('#myWindowShow').show();
                    }
                },
                error: function(msg){}
            });*/
        });

        $('#aaa').on('click',function (){
            $.ajax({
                url:'admin/flash',
                type:'get',
                success:function(res){
                    if(res.data == 1){
                        layer.tip('aaa');
                    }
                },
                error: function(msg){

                }
            });
        });
    });

    //获取剩余的牌数
    function getTotal() {
        var total = 0;
        $.ajax({
            url:'getTotal',
            type:'get',
            async:false,
            success:function(res){
                if(res.code == 200){
                    total = res.data.total;
                }
            },
            error: function(msg){

            }
        });
        return total;
    }

    function getSelfCard() {
        var selfCard = '';
        $.ajax({
            url:'getSelfCard?name=' + username,
            type:'get',
            async:false,
            success:function(res){
                if(res.code == 200){
                    selfCard = res.data.selfCard;
                }
            },
            error: function(msg){}
        });
        return selfCard;
    }

    //用户注册方法
    function userRegister(layer) {
        var flag = true;
        $.ajax({
            url:'userRegister?name=' + username,
            type:'get',
            async:false,
            success:function(res){
                if(res.code == 500){
                    //layer.msg(res.message);
                    flag = false;
                }
            },
            error: function(msg){}
        });
        return flag;
    }

    function isChecked(isChecked) {
        var model = '';
        $.ajax({
            url:'isChecked?model=' + isChecked,
            type:'get',
            async:false,
            success:function(res){
                if(res.code == 200){
                    model = res.data;
                }
            },
            error: function(msg){}
        });
        return model;
    }
</script>
</html>

